package com.sky.financelake.retrofit;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.sky.financelake.bean.dto.CommunityDto;
import com.sky.financelake.bean.dto.CommunityDto1;
import com.sky.financelake.bean.entity.CommunityEntity;
import com.sky.financelake.bean.param.CommunityParam;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommunityRepository extends BaseRepository{


    private static CommunityRepository communityRepository;

    public static CommunityRepository getInstance() {
        if (communityRepository == null) {
            communityRepository = new CommunityRepository();
        }
        return communityRepository;
    }

    private Api api;

    public CommunityRepository() {
        api = RetrofitService.cteateService(Api.class);
    }


    public void getCommunityPagingList(MutableLiveData data, CommunityParam param) {
        Log.i(TAG, "getCommunityPagingList: ");
        api.getCommunityPagingList(param).enqueue(new Callback<ApiResponse<List<CommunityDto>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<CommunityDto>>> call, Response<ApiResponse<List<CommunityDto>>> response) {
                if (response.body() != null) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<CommunityDto>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getCommunityPagingList1(MutableLiveData data, CommunityParam param) {
        Log.i(TAG, "getCommunityPagingList: ");
        api.getCommunityPagingList1(param).enqueue(new Callback<ApiResponse<List<CommunityDto1>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<CommunityDto1>>> call, Response<ApiResponse<List<CommunityDto1>>> response) {
                if (response.body() != null) {
                    data.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<List<CommunityDto1>>> call, Throwable t) {
                data.setValue(null);
            }
        });
    }

    public void getCommunityById(MutableLiveData liveData, Integer id) {
        api.getArticleById(id).enqueue(new Callback<ApiResponse<CommunityEntity>>() {
            @Override
            public void onResponse(Call<ApiResponse<CommunityEntity>> call, Response<ApiResponse<CommunityEntity>> response) {
                if (response.body() != null) {
                    liveData.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<ApiResponse<CommunityEntity>> call, Throwable t) {
                Log.i(TAG, "onFailure: ");
                liveData.setValue(null);
            }
        });
    }

}
